/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.time.LocalDate;
import java.util.Random;

/**
 * <p>Project: javase2203 - Switch4
 * <p>Powered by webrx On 2022-07-14 09:02:54
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Switch4 {
    public static void main(String[] args) {
        Random rand = new Random();
        //switch表达式 java12
        int yy = rand.nextInt(2000, 2023);
        int mm = rand.nextInt(1, 13);

        // ->  2-> case 4,6,9->16
        int days = switch (mm) {
            //case 2 -> LocalDate.of(yy, mm, 1).isLeapYear() ? 29 : 28;

            case 2->{
                int dd = 0;
                if(LocalDate.of(yy, mm, 1).isLeapYear()){
                    dd = 29;
                }else{
                    dd = 28;
                }
                yield dd;
            }

            case 4, 6, 9, 11 -> 30;
            //default 必须有 -> 31代表返回值
            //default -> 31;
            default -> {
                int i = 3;
                yield 31; //如果default 或个开关有多行语句块，在块中必须为yield关键用于返回值
            }

        };
        System.out.printf("%04d年%02d月有%d天%n", yy, mm, days);
    }
}
